package office.service.hr.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import office.action.hr.form.CheckIdeaForm;
import office.action.hr.form.PurchaseEntryForm;
import office.bo.hr.impl.CheckFolw;
import office.bo.hr.impl.Officesupplies;
import office.bo.hr.impl.PurchaseEntry;
import office.bo.person.LeaveRequest;
import office.domainset.BizDomainProviderSetImpl;
import office.service.finance.impl.CheckFolwServiceImpl;
import office.service.hr.IPurchaseEntryService;
import platform.common.exception.BusinessException;

public class PurchaseEntryServiceImpl extends CheckFolwServiceImpl implements IPurchaseEntryService{

	@Override
	public CheckFolw getCheckFolwById(String id) throws BusinessException {
		PurchaseEntry purchaseEntry = null;
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			purchaseEntry = (PurchaseEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getPurchaseEntryDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取审批对象信息失败!", e);
		}
		return purchaseEntry;
	}
	
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public PurchaseEntryForm getById(String id) throws BusinessException {
		PurchaseEntryForm purchaseEntryForm = new PurchaseEntryForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			PurchaseEntry purchaseEntry = (PurchaseEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getPurchaseEntryDP(), id);
			purchaseEntryForm.setId(purchaseEntry.getId().toString());
			purchaseEntryForm.setPurchaseNumber(purchaseEntry.getPurchaseNumber());
			purchaseEntryForm.setPrice(purchaseEntry.getPrice());
			purchaseEntryForm.setOfficesuppliesId(purchaseEntry.getOfficesupplies().getId().toString());
			purchaseEntryForm.setState(purchaseEntry.getState());
			purchaseEntryForm.setEntryRole(purchaseEntry.getEntryRole());
			purchaseEntryForm.setEntryUserId(purchaseEntry.getUserId());
			if(null!=purchaseEntry.getCheckRole()){
				purchaseEntryForm.setCheckRole(purchaseEntry.getCheckRole());
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取采购对象失败!", e);
		}
		return purchaseEntryForm;
	}
	/**
	 * 编辑/保存采购信息
	 * @param purchaseEntryForm
	 * @throws BusinessException
	 */
	public void editPurchaseEntry(PurchaseEntryForm purchaseEntryForm)throws BusinessException {
		try {
			PurchaseEntry purchaseEntry = null;
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			if(null==purchaseEntryForm.getId() || ("").equals(purchaseEntryForm.getId())){
				purchaseEntry = (PurchaseEntry)domainProviderSetImpl.getPurchaseEntryDP().newEmptyObj();
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				purchaseEntry.setEntryDate(dateFmt.format(c.getTime()));
				purchaseEntry.setEntryRole(purchaseEntryForm.getEntryRole());
				purchaseEntry.setUserId(purchaseEntryForm.getEntryUserId());
				Officesupplies officesupplies = (Officesupplies)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesDP(), purchaseEntryForm.getOfficesuppliesId());
				purchaseEntry.setOfficesupplies(officesupplies);
			}else{
				purchaseEntry = (PurchaseEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getPurchaseEntryDP(), purchaseEntryForm.getId());
			}
			purchaseEntry.setPurchaseNumber(purchaseEntryForm.getPurchaseNumber());
			purchaseEntry.setPrice(purchaseEntryForm.getPrice());
			purchaseEntry.setState("0");//等待审批
			purchaseEntry.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("保存采购对象失败!", e);
		}
	}
	/**
	 * 获取采购列表
	 * @return
	 * @throws BusinessException
	 */
	public List getPurchaseEntryList()throws BusinessException {
		List listData = new ArrayList();
		try {
			PurchaseEntry purchaseEntry = null;
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getPurchaseEntryDP(), "findPurchaseEntry", new HashMap());
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("保存采购对象失败!", e);
		}
		return listData;
	}
	
	/**
	 * 删除采购信息
	 * @throws BusinessException
	 */
	public void delData(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().deleteById(domainProviderSetImpl.getPurchaseEntryDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("删除采购对象失败!", e);
		}
	}
	
	/**
	 * 
	 * @param check 审批是否为通过
	 * @param id
	 * @throws BusinessException
	 */
	public void checkData(boolean check,String id,String proKey)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			PurchaseEntry purchaseEntry = (PurchaseEntry)this.getCheckFolwById(id);
			if(check){
				String nextRole = getNextCheckRole(purchaseEntry.getEntryRole(),purchaseEntry.getCheckRole(),proKey);
				if(nextRole.equals("end")){
					purchaseEntry.setState("100");
					purchaseEntry.setCheckRole("");
					//给对应的办公用品增加采购数量
					Officesupplies officesupplies = (Officesupplies)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesDP(), purchaseEntry.getOfficesupplies().getId());
					officesupplies.setRepertory(officesupplies.getRepertory()+purchaseEntry.getPurchaseNumber());
					officesupplies.store(domainProviderSet);
				}else{
					purchaseEntry.setCheckRole(nextRole);
				}
			}else{
				purchaseEntry.setCheckRole("");//重新提交审批,CheckRole重新赋值
				purchaseEntry.setState("-1");//审批驳回
			}
			
			purchaseEntry.store(domainProviderSet);//保存对象
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("设置审批状态信息对象失败!", e);
		}
	}

}
